﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using System.IO;

namespace BskLeague.Controllers
{
    
    public class AnnounceController : Controller
    {
        BskLeagueDB _db = new BskLeagueDB();

        //
        // GET: /Announce/

        public ActionResult Index()
        {
           // AnnounceCommunicate communicate = new AnnounceCommunicate { AnnounceDate = DateTime.Now };
            List<AnnounceCost> announceCosts=_db.AnnounceCosts.ToList();
            //List<int> costs = new List<int>(20);


            int[] Costs;
            Costs = new int[20];

            foreach(var a in announceCosts)
            {
                Costs[a.NumberOfPlayers] = a.Cost;
                
            }

            ViewBag.Costs = Costs;
            return View();
        }

        [HttpPost]
        public ActionResult Index([Bind(Exclude = "AnnounceCommunicateId, AnnounceDate")]AnnounceCommunicate communicate, HttpPostedFileBase file)
        {
            communicate.AnnounceDate = DateTime.Now;
            
            communicate.Status = _db.AnnounceCommunicateStatuses.Single(s => s.Status == "Posted");
            if (ModelState.IsValid)
            {
                communicate.SavePaymentFile(Server, file);
                _db.AnnounceCommunicates.Add(communicate);
                foreach (var p in communicate.Players)
                {
                    p.Licence = _db.Licences.SingleOrDefault(l => l.LicenceId == p.LicenceId);
                    if (p.Licence != null)
                    {
                        var player = _db.Players.Include("Licence").SingleOrDefault(pl => pl.LicenceId == p.LicenceId);
                        _db.Communicates.Add(new Communicate { Sender = "Admin", Receiver = player.UserName, 
                            Content = "Zostałeś zgłoszony do drużyny " + communicate.TeamName, Date=DateTime.Now });
                    }
                }

                _db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            else
            {
               return View(communicate);
            }

            //return "Tutaj PDF!";

        }

        [HttpPost] //z panelu admina linki odrazu do post?
        public ActionResult Confirm(int AnnounceIndex) //lub announce hash?
        {
            var communicate = _db.AnnounceCommunicates.Single(c => c.AnnounceCommunicateId == AnnounceIndex);

            if (null != communicate)
            {
               // communicate.ConfirmAnnouncement(_db);
            }

            return RedirectToAction("Index", "Home");
        }

    }
}
